<?php
/*
Overmundo, cultura de todo o Brasil
Desenvolvido pela Tecnopop para o Núcleo de Idéias Movimento
Copyright (C) 2006  Núcleo de Idéias Movimento

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

$nivelPagina = "desconhecido";

include('../lib/checa_login.php');
 
 
class_exists('Site') || require('../lib/class.site.php');
class_exists('Overmundo') || require('../lib/class.overmundo.php');
class_exists('Conecta') || require('../lib/class.conecta.php');
class_exists('Usuario') || require('../lib/class.usuario.php');
class_exists('Produto') || require('../lib/class.produto.php');

require_once "HTTP/Request.php";

include('../lib/config_hw_s3.php');

session_start();
if (!isset($minhaConexao))  {
	$minhaConexao = new Conecta();
}
if (!isset($meuSite)) {
	$meuSite = new Overmundo();
}

if (!isset($meuSite->linkid)) { 
	$meuSite->linkid = $minhaConexao->linkid;
}


$codProduto = $meuSite->OvermundoProcessaFilaVideo();

if($codProduto > 0){
		
	$thisProduto = new Produto($meuSite->linkid, $codProduto);
	
	$thisProduto->SetaFilaVideo("convertendo");
	$thisProduto->update();
	
	$nomeVideo = $thisProduto->PegaProduto();
	$pathVideo =  $overBaseUrlVideo . $nomeVideo;
	
	$nomeVideo_id = substr($nomeVideo, 0, strrpos($nomeVideo,"."));
	
	$req =& new HTTP_Request($hwUrlDiscover);
	 
	$req->setBasicAuth($hwLogin, $hwPasswd);
	
	$req->setMethod(HTTP_REQUEST_METHOD_POST);
	$req->addPostData('url', $pathVideo);
	$req->addPostData('codProduto', $codProduto);
	$req->addPostData('download','true');
	$req->addPostData('ping_url_after_encode','http://www.overmundo.com.br/_banco/heywatch.php?id='.$codProduto.'&status=convertido');
	$req->addPostData('ping_url_if_error','http://www.overmundo.com.br/_banco/heywatch.php?id='.$codProduto.'&status=erro');
	$req->addPostData('automatic_encode','true');
	$req->addPostData('s3_directive','s3://' . $s3Id . ':' . $s3Key . '@' . $s3Bucket . '/'.$nomeVideo_id.".flv");    //*set the amazon s3 container (sonyhd_query)  and the final object *//
	
	$response = $req->sendRequest();
	
	
	if (PEAR::isError($response)) {
		echo $response->getMessage();
	} else {
		echo $req->getResponseBody();
	}

}


//load PEAR HTTP Request Library
//include("HTTP/Request.php");

/*
//get heywatch video ids
$hw_video_id = $_POST["video_id"];
$hw_encoded_video_id = $_POST["encoded_video_id"];

//HANDLE SCREENSHOT ////////////////

//download encoded video screenshot from heywatch service
$req =& new HTTP_Request("http://heywatch.com/encoded_video/".$hw_encoded_video_id.".jpg");
$req->setBasicAuth("MY_USERNAME", "MY_PASSWORD");
$req->sendRequest();
$real_url = $req->getResponseHeader("location");
$video_screenshot_content = file_get_contents($real_url);

//write video screenshot to temp path
$video_screenshot_path = DOC_ROOT."/heywatch/thumb_temp/".$hw_encoded_video_id.".jpg";
$video_screenshot = fopen($video_screenshot_path, "wb");
fwrite($video_screenshot, $video_screenshot_content);
fclose($video_screenshot);

//HANDLE VIDEO FILE ////////////////

//download encoded video from heywatch service
$req =& new HTTP_Request("http://heywatch.com/encoded_video/".$hw_encoded_video_id.".bin");
$req->setBasicAuth("MY_USERNAME", "MY_PASSWORD");
$req->sendRequest();
$real_url = $req->getResponseHeader("location");
$video_content = file_get_contents($real_url);

//write video to temp path
$video_file_path = DOC_ROOT."/heywatch/vid_temp/".$hw_encoded_video_id.".flv";
$video_file = fopen($video_file_path, "wb");
fwrite($video_file, $video_content);
fclose($video_file);

//get video content using CURL library 
//o codigo acima tinha um erro, o cara arrumou assim:
//For some reason the "file_get_contents" function wasnt working for me, 
//so I used the CURL library, which isn't always configured 
//(so you need to do that separately). And then I used the following code.

$ch = curl_init($real_url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$video_content = curl_exec($ch);
curl_close($ch);
* */

/*
<?php
	require_once "HTTP/Request.php";
	$req =& new HTTP_Request("http://heywatch.com/encoded_video/1000.bin");
	$req->setBasicAuth("johndoe", "foo");
	$req->sendRequest();
	$real_url = $req->getResponseHeader("location");
	$video_content = file_get_contents($real_url);
	$file = fopen('myvideo.flv', 'wb');
	fwrite($file, $video_content);
	fclose($file);
?>
 */
//
//require_once "HTTP/Request.php";
//
////$req =& new HTTP_Request("http://heywatch.com/job/");
//$req =& new HTTP_Request("http://heywatch.com/account");
// 
//$req->setBasicAuth("overmundovideo", "servideosover2007");
//
//$req->setMethod(HTTP_REQUEST_METHOD_GET);
//
//$req->sendRequest();
//
//$real_url = $req->getResponseHeader("location");
//echo $real_url;
//$video_content = file_get_contents($real_url);
//echo $video_content;

//echo $req->getResponseCode();
//echo $req->getResponseBody();
//
//
//foreach ($req->getResponseHeader() as $name => $value) {
//    echo $name . " = " . $value . "\n";
//}

//$result = $req->addFile("johndoe-txt.pdf", "/home/johndoe/text.pdf");
//
//if (PEAR::isError($result)) {
//    echo $result->getMessage();
//} else {
//
//    $response = $req->sendRequest();
//
//    if (PEAR::isError($response)) {
//        echo $response->getMessage();
//    } else {
//        echo $req->getResponseBody();
//    }
//}


?>